{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "constitutional-momentum",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/largest-time-for-given-digits/\n",
    "\n",
    "\n",
    "Runtime: 0 ms, faster than 100.00% of C++ online submissions for Largest Time for Given Digits.\n",
    "Memory Usage: 9.6 MB, less than 48.52% of C++ online submissions for Largest Time for Given Digits.\n",
    "\n",
    "\n",
    "\n",
    "```cpp\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <cmath>\n",
    "#include <sstream>\n",
    "#include <iostream>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    string format_the_time(int hour, int minites) {\n",
    "        auto h = to_string(hour);\n",
    "        auto m = to_string(minites);\n",
    "        if (h.size() == 1) {\n",
    "            h = \"0\" + h;\n",
    "        }\n",
    "        if (m.size() == 1) {\n",
    "            m = \"0\" + m;\n",
    "        }\n",
    "        return h+\":\"+m;\n",
    "    }\n",
    "\n",
    "    string largestTimeFromDigits(vector<int>& arr) {\n",
    "        //6:19\n",
    "        sort(arr.begin(), arr.end());\n",
    "        int the_longest_time = -1;\n",
    "        int the_longest_hour = -1;\n",
    "        int the_longest_minutes = -1;\n",
    "        do {\n",
    "            int zero = arr[0];\n",
    "            int one = arr[1];\n",
    "            int two = arr[2];\n",
    "            int three = arr[3];\n",
    "\n",
    "            int hour = zero * 10 + one;\n",
    "            int minites = two * 10 + three;\n",
    "\n",
    "            //cout << hour << \":\" << minites << endl;\n",
    "\n",
    "            if (((hour >=0) && (hour <= 23)) && ((minites >=0) && (minites <= 59))) {\n",
    "                int length = hour * 60 + minites;\n",
    "                if (length > the_longest_time) {\n",
    "                    the_longest_time = length;\n",
    "                    the_longest_hour = hour;\n",
    "                    the_longest_minutes = minites;\n",
    "                }\n",
    "            }\n",
    "\n",
    "        } while (std::next_permutation(arr.begin(), arr.end()));\n",
    "        if (the_longest_time == -1) {\n",
    "            return \"\";\n",
    "        } else {\n",
    "            return format_the_time(the_longest_hour, the_longest_minutes);\n",
    "        }\n",
    "        //6:33\n",
    "        //there has a bug of next_permutation, you need to sort your vector before you send it to the permutation function. \n",
    "        // https://stackoverflow.com/questions/31258426/why-does-next-permutation-skip-some-permutations\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "racial-nirvana",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
